﻿// Author: AustralTek 
// Create date: 2010/05/04
// http://www.australtek.com
// Copyright: 2009 - 2010

(function () {
    try {
        function _TimePicker(obj, options) {
            try {
                /*--------------------------*/
                /*----Private properties----*/
                /*--------------------------*/
                var containerAux;
                var container;
                var timePickerObj;
                var privateParameters;

                /* Sets and Overrides certains properties.*/
                function _initialize(obj, options) {
                    try {
                        containerAux = obj;
                        container = null;
                        timePickerObj = null;

                        privateParameters = new PrivateParameters(options);
                        privateParameters.TimeFormat = 24; //24 hours
                        privateParameters.Time = ((!privateParameters.Time) || (!_validateTime(privateParameters.Time)))
                                                        ? _setDefaultValue()
                                                        : privateParameters.Time;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _redraw() {
                    try {
                        container = document.createElement('div');
                        container.className = 'timePickerContainers';
                        containerAux.appendChild(container);

                        timePickerObj = document.createElement('input');
                        timePickerObj.className = 'timePickerInputs';
                        timePickerObj.type = 'text';
                        timePickerObj.value = privateParameters.Time;
                        container.appendChild(timePickerObj);

                        addEventHandler(timePickerObj, 'onclick', _timePickerObjClick);
                        addEventHandler(timePickerObj, 'onkeydown', _timePickerObjKeydown);
                        addEventHandler(timePickerObj, 'onblur', _timePickerObjBlur);
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                /*-----------------------*/
                /*----Private methods----*/
                /*-----------------------*/

                function _setValue(newTime) {
                    var valid = false;
                    var newTimeValue;
                    try {
                        if (newTime instanceof Date) {
                            newTimeValue = ((newTime.getHours() < 10) ? '0' + newTime.getHours() : newTime.getHours())
                                            + ':' + ((newTime.getMinutes() < 10) ? '0' + newTime.getMinutes() : newTime.getMinutes())
                                            + ':' + ((newTime.getSeconds() < 10) ? '0' + newTime.getSeconds() : newTime.getSeconds());
                        }
                        else {
                            var tempValue = new Object();
                            for (var param in newTime)
                                tempValue[param] = newTime[param];
                            newTime.hours = (!newTime.hours)
                                            ? timePickerObj.value.slice(0, 2)
                                            : ((newTime.hours >= 10) ? newTime.hours.toString() : '0' + newTime.hours.toString());
                            newTime.minutes = (!newTime.minutes)
                                            ? timePickerObj.value.slice(3, 5)
                                            : ((newTime.minutes >= 10) ? newTime.minutes.toString() : '0' + newTime.minutes.toString());
                            newTime.seconds = (!newTime.seconds)
                                            ? timePickerObj.value.slice(3, 5)
                                            : ((newTime.seconds >= 10) ? newTime.seconds.toString() : '0' + newTime.seconds.toString());

                            var newTimeValue = newTime.hours + ':' + newTime.minutes + ':' + newTime.seconds;
                        }
                        if ((_validateTime(newTimeValue)) && (!timePickerObj.value != newTimeValue)) {
                            _normalizeAndSetNewTime(newTimeValue);
                            valid = true;
                            //                                privateParameters.Events.onChange();
                        }
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                    return valid;
                }

                function _setDefaultValue() {
                    try {
                        var todayDate = new Date();
                        var h = todayDate.getHours();
                        var m = todayDate.getMinutes();
                        var s = todayDate.getSeconds();
                        h = (h < 10) ? '0' + h.toString() : h.toString();
                        m = (m < 10) ? '0' + m.toString() : m.toString();
                        s = (s < 10) ? '0' + s.toString() : s.toString();

                        return h + ':' + m + ':' + s;
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _validateTime(newTime) {
                    try {
                        if ((newTime.match('^([0-9]{2})(([:.]?[0-9]{2}){2})$'))) {
                            newTime = newTime.replace(/[^0-9]/g, '');
                            if ((parseInt(newTime) < parseInt(privateParameters.TimeFormat) * 10000) && (parseInt(newTime.substring(2)) < 6000) && (parseInt(newTime.slice(4)) < 60))
                                return true;
                        }
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                    return false;
                }

                function _normalizeAndSetNewTime(newTime) {
                    try {
                        newTime = newTime.replace(/[^0-9]/g, '');
                        timePickerObj.value = privateParameters.Time = newTime.substring(0, 2) + ':' + newTime.substring(2, 4) + ':' + newTime.substring(4, 6);
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _getResults(options) {
                    var ret = null;
                    try {
                        options = options || { DBEngine: 'SQLServer', paramsWishList: null }; //DBEngine = SQL Server by default.
                        options.DBEngine = options.DBEngine || 'SQLServer';

                        var DBType = 'varchar'; //SQLServer type by default

                        switch (options.DBEngine.toUpperCase()) {
                            case 'SQLSERVER': break;
                            case 'ORACLE': DBType = 'varchar2'; break;
                            default: throw Error;
                        };

                        ret = new QueryParameters({ DBEngine: options.DBEngine });

                        ret.Add('Time', DBType, timePickerObj.value);
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                    return ret;
                }

                /*--------------------*/
                /* ----- Events ----- */
                /*--------------------*/
                function _timePickerObjClick(e) {
                    try {
                        var target = e.srcElement || e.target;
                        target.select();
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _timePickerObjKeydown(e) {
                    try {
                        var target = e.srcElement || e.target;
                        switch (true) {
                            case (e.keyCode == 13):
                                if (!_validateTime(timePickerObj.value))
                                    timePickerObj.value = privateParameters.Time;
                                else if (privateParameters.Time != timePickerObj.value) {
                                    _normalizeAndSetNewTime(timePickerObj.value);
                                    privateParameters.Events.onChange();
                                }
                                break;
                        }
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                function _timePickerObjBlur() {
                    try {
                        if (!_validateTime(timePickerObj.value))
                            timePickerObj.value = privateParameters.Time;
                        else if (privateParameters.Time != timePickerObj.value) {
                            _normalizeAndSetNewTime(timePickerObj.value);
                            privateParameters.Events.onChange();
                        }
                    }
                    catch (Error) { privateParameters.Events.onError(Error); }
                }

                /*----------------*/
                /*-----Public-----*/
                /*----------------*/

                /*----------------------*/
                /*--------Methods-------*/
                /*----------------------*/
                this.GetResults = function (options) {
                    return _getResults(options);
                };
                this.SetValue = function (newTime) {
                    _setValue(newTime);
                };

                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                try {
                    /* Sets properties.*/
                    _initialize(obj, options);
                    _redraw();
                }
                catch (Error) { privateParameters.Events.onError(Error); }
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
                /*-----------------------------------------------------------------------*/
            }
            catch (Error) { window.status = Error.Message; }
        }

        window.TimePicker = function (obj, instanceOptions) { return new _TimePicker(obj, instanceOptions); };
    }
    catch (Error) { window.status = Error.Message; }
})();